package server.child.thread.handler;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import qq.message.DataPackage;
import qq.message.DataPackage.MessageType;
import server.child.thread.ServerChildThread;
import server.handler.ServerHandler;

public class ChildThreadHandle {
	
	public void handleLogout(DataPackage data) throws IOException{
		
		//返回退出确认信息
		DataPackage logoutBack=new DataPackage();
		logoutBack.setFromName("server");
		logoutBack.setMessageData("");
		logoutBack.setToName(data.getFromName());
		logoutBack.setMessageType(MessageType.LOGOUT);
		logoutBack.setTime(System.currentTimeMillis());
		ServerHandler.userMap.get(data.getFromName()).getOos().writeObject(logoutBack);
		//关闭要退出的的socket
		(ServerHandler.userMap.get(data.getFromName())).getSocket().close();
		//客户端映射中删除该子线程
		ServerHandler.userMap.remove(data.getFromName());
		//遍历发送最新的还存活的client的东西
		Iterator iter = ServerHandler.userMap.entrySet().iterator();
		Set nameSet=ServerHandler.userMap.keySet();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String name = (String) entry.getKey();
			DataPackage temp=new DataPackage();
			temp.setFromName(data.getFromName());
			temp.setToName(name);
			temp.setMessageType(MessageType.LIST);
			temp.setMessageData(nameSet.toString());
			temp.setTime(System.currentTimeMillis());
			ServerChildThread tempSch=(ServerChildThread)entry.getValue();
			tempSch.getOos().writeObject(temp);
		}
	}
	/**
	 * 根据data里面的toName值判断是否是群发，反斜杠为群发
	 * @throws IOException 
	 */
	public void handleMessage(DataPackage data) throws IOException{
		String targetName=data.getToName();
		if(targetName.equals("\\"))
		{
			//获取map的迭代器
			Iterator iter = ServerHandler.userMap.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				ServerChildThread tempSch=(ServerChildThread)entry.getValue();
				tempSch.getOos().writeObject(data);
			}
		}
		else
		{
			if(ServerHandler.userMap.containsKey(targetName))
			{
				ServerHandler.userMap.get(targetName).getOos().writeObject(data);
			}
			else
				/*
				 *可以添加缓存器，在该用户上线的时候直接推送给他 
				 */
				System.out.println("client "+targetName+" out of line");
		}
			
	}
	/**
	 * 处理震动类信息
	 * @throws IOException 
	 */
	public void handleShake(DataPackage data) throws IOException{
		String targetName=data.getToName();
		ServerHandler.userMap.get(targetName).getOos().writeObject(data);
	}
	/**
	 * 理论不存在
	 * @throws IOException 
	 */
	public static void handleLogin(DataPackage data) throws IOException{
		/*
		 * 添加对客户端logout请求的回应
		 * 客户端确认logout回应后方可关闭
		 */
		Iterator iter = ServerHandler.userMap.entrySet().iterator();
		DataPackage dataTemp=new DataPackage();
		dataTemp.setMessageType(MessageType.LIST);
		dataTemp.setMessageData(ServerHandler.userMap.entrySet().toString());
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			ServerChildThread tempSch=(ServerChildThread)entry.getValue();
			tempSch.getOos().writeObject(dataTemp);
		}
	}
	/**
	 * 理论不存在
	 */
	public void handleList(DataPackage data){
		System.out.println("login type....................");;
	}
}
